﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Web7.Extensions;

namespace Web7.Core.Apps
{
    public static class AppRegistryExtensions
    {
        /// <summary>
        /// 注册模块的路由配置
        /// <remarks>
        /// 后加入的模块的路由配置将排在前面，目的是提供一种"覆盖"机制
        /// </remarks>
        /// </summary>
        /// <param name="appsLoaded"></param>
        public static void RegisterRoutes(this IAppRegistry appsLoaded)
        {
            IEnumerable<IApp> apps = appsLoaded.GetApps().Reverse();

            foreach (IApp app in apps)
                app.RegisterRoutes();
        }

        /// <summary>
        /// 获取当前模块所属于的认证模块
        /// </summary>
        /// <param name="appsLoaded"></param>
        /// <param name="currentAppName"></param>
        /// <returns></returns>
        public static IAuthenticationApp GetAuthenticationApp(this IAppRegistry appsLoaded, string currentAppName)
        {
            if (appsLoaded == null || currentAppName.IsNullOrWhiteSpace()) return null;

            //当前模块
            IApp currentApp = appsLoaded.GetApps<IApp>().Where(m => m.AppName == currentAppName).FirstOrDefault();
            if (currentApp == null) return null;

            //当前模块的认证模块
            IAuthenticationApp authenticationApp = appsLoaded.GetApps<IAuthenticationApp>().FirstOrDefault(); ///Where(m => m.GetType().ToString() == currentApp.AuthenticationAppName).
            return authenticationApp;
        }
    }
}
